package com.geek.projects.user.service.impl;

import com.geek.projects.user.domain.User;
import com.geek.projects.user.repository.UserRepository;
import com.geek.projects.user.repository.UserRepositoryImpl;
import com.geek.projects.user.service.UserService;
import com.geek.projects.user.sql.DBConnectionManager;
import com.geek.projects.utils.MapToObjectUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;


/**
 * @author lizhiyang
 */
public class UserServiceImpl implements UserService {

    private UserRepository userRepository = new UserRepositoryImpl();



    @Override
    public void login(Map<String, Object> params) throws SQLException {
        User user;
        try {
            user = MapToObjectUtil.mapToObjectUtil(params, new User());
        } catch (IllegalAccessException e) {
            throw new RuntimeException("数据有误,请正确输入信息");
        }

        User queryUser = userRepository.queryOne(user);

        if (null == queryUser) {
            throw new RuntimeException("用户不存在,请检查用户信息");
        }
        String queryUserPassword = queryUser.getPassword();
        String inputUserPassword = user.getPassword();
        if (!queryUserPassword.equals(inputUserPassword)) {
            throw new RuntimeException("密码不匹配,请重新输入");
        }
        //success
    }

    @Override
    public void register(Map<String, Object> params) throws SQLException {
        User user;
        try {
            user = MapToObjectUtil.mapToObjectUtil(params, new User());
        } catch (IllegalAccessException e) {
            throw new RuntimeException("数据有误,请正确输入信息");
        }
        if (null != userRepository.queryOne(user)) {
            throw new RuntimeException("用户已存在");
        }
        //进行新增
        userRepository.save(user);
        //success
    }
}
